package org.example.pack1023.reflect;

import java.lang.reflect.Constructor;
import java.util.Arrays;

public class DemoB {
    public static void main(String[] args) throws Exception {
        Class c = Class.forName("org.example.pack1023.reflect.Users");

        //获取到构造器
        Constructor[] constructors1 = c.getConstructors();
        Constructor[] constructors2 = c.getDeclaredConstructors();
        System.out.println(Arrays.toString(constructors1));
        System.out.println(Arrays.toString(constructors2));

        Class[] 形式参数列表1 = {};
        Class[] 形式参数列表2 = {Integer.TYPE};
        Class[] 形式参数列表3 = {Integer.TYPE,String.class};
        Constructor constructor1 = c.getConstructor(形式参数列表1);
        Constructor constructor2 = c.getDeclaredConstructor(形式参数列表2);
        Constructor constructor3 = c.getDeclaredConstructor(形式参数列表3);
        System.out.println(constructor1);
        System.out.println(constructor2);
        System.out.println(constructor3);

        //调用构造器，获取对象并初始化
        Object obj1 = constructor2.newInstance(67);
        System.out.println(obj1);
        //缩写形式(只能用于无参构造器)
        Object obj2 = c.newInstance();
        System.out.println(obj2);
    }
}
